<?php /* 	These code was generated using phpCIGen v 0.1.a (21/04/2009)
	#zaqi 		zaqi.smart@gmail.com,http://zenzaqi.blogspot.com,
    #songbee	mukhlisona@gmail.com
	#CV. Trust Solution, jl. Saronojiwo 19 Surabaya, http://www.ts.co.id

	+ Module  		: m_user Model
	+ Description	: For record model process back-end
	+ Filename 		: c_m_user.php
 	+ creator 		: 
 	+ Created on 06/Mar/2012 12:35:49

*/

class M_m_user extends Model{

		//constructor
		function M_m_user() {
			parent::Model();
		}

		//function for get list record
		function m_user_list($filter,$start,$end){
			$sql = "SELECT user_id,
							 user_name,
							 user_password,
							 m_staff_id,
							 m_usergroup_id,
							 user_active					  FROM 	 m_user";

			// For simple search
			if ($filter<>""){
				$sql .=eregi("WHERE",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(user_id) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(user_name) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(user_password) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(m_staff_id) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(m_usergroup_id) LIKE '%".addslashes(strtolower($filter))."%' OR
							lower(user_active) LIKE '%".addslashes(strtolower($filter))."%' )";
			}

			$result = $this->db->query($sql);
			$nbrows = $result->num_rows();
			$limit = $sql." OFFSET ".$start." LIMIT ".$end;
			$result = $this->db->query($limit);

			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}

		//function for create new record
		function m_user_create($user_name,
				 $user_password,
				 $m_staff_id,
				 $m_usergroup_id,
				 $user_active){
			$data = array(
						 "user_name"=>$user_name,						 "user_password"=>$user_password,						 "m_staff_id"=>$m_staff_id,						 "m_usergroup_id"=>$m_usergroup_id,						 "user_active"=>$user_active					);
			$this->db->insert('m_user', $data);
			if($this->db->affected_rows())
				return '1';
			else
				return '0';
		}

		//function for update record
		function m_user_update($user_id,
				 $user_name,
				 $user_password,
				 $m_staff_id,
				 $m_usergroup_id,
				 $user_active){
			$data = array(
						"user_name"=>$user_name,						"user_password"=>$user_password,						"m_staff_id"=>$m_staff_id,						"m_usergroup_id"=>$m_usergroup_id,						"user_active"=>$user_active					);

			$this->db->where('user_id', $user_id);
			$this->db->update('m_user', $data);
			return '1';
		}

		//fcuntion for delete record
		function m_user_delete($pkid){
			// You could do some checkups here and return '0' or other error consts.
			// Make a single query to delete all of the m_users at the same time :
			if(sizeof($pkid)<1){
				return '0';
			} else if (sizeof($pkid) == 1){
				$sql = "DELETE FROM m_user WHERE user_id = ".$pkid[0];
				$this->db->query($sql);
			} else {
				$sql = "DELETE FROM m_user WHERE ";
				for($i = 0; $i < sizeof($pkid); $i++){
					$sql = $sql . "user_id= ".$pkid[$i];
					if($i<sizeof($pkid)-1){
						$sql = $sql . " OR ";
					}
				}
				$this->db->query($sql);
			}
			if($this->db->affected_rows()>0)
				return '1';
			else
				return '0';
		}

		//function for advanced search record
		function m_user_search($user_id,
				 $user_name,
				 $user_password,
				 $m_staff_id,
				 $m_usergroup_id,
				 $user_active,$start,$end){
			//full query
			$sql = "SELECT user_id,
							 user_name,
							 user_password,
							 m_staff_id,
							 m_usergroup_id,
							 user_active					  FROM 	 m_user";

			if($user_id!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(user_id) LIKE '%".addslashes(strtolower($user_id))."%'";
			};
			if($user_name!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(user_name) LIKE '%".addslashes(strtolower($user_name))."%'";
			};
			if($user_password!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(user_password) LIKE '%".addslashes(strtolower($user_password))."%'";
			};
			if($m_staff_id!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(m_staff_id) LIKE '%".addslashes(strtolower($m_staff_id))."%'";
			};
			if($m_usergroup_id!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(m_usergroup_id) LIKE '%".addslashes(strtolower($m_usergroup_id))."%'";
			};
			if($user_active!=''){
				$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
				$sql.= " lower(user_active) LIKE '%".addslashes(strtolower($user_active))."%'";
			};

			$result = $this->db->query($sql);
			$nbrows = $result->num_rows();
			$limit = $sql." OFFSET ".$start." LIMIT ".$end;
			$result = $this->db->query($limit);

			if($nbrows>0){
				foreach($result->result() as $row){
					$arr[] = $row;
				}
				$jsonresult = json_encode($arr);
				return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
			} else {
				return '({"total":"0", "results":""})';
			}
		}

		//function for print record
		function m_user_print($user_id,
				 $user_name,
				 $user_password,
				 $m_staff_id,
				 $m_usergroup_id,
				 $user_active,$option,$filter){
			//full query
			$sql = "SELECT user_id,
							 user_name,
							 user_password,
							 m_staff_id,
							 m_usergroup_id,
							 user_active					  FROM 	 m_user";
			if($option=='LIST'){
				$sql .=eregi("WHERE",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(user_id) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(user_name) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(user_password) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(m_staff_id) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(m_usergroup_id) LIKE '%".addslashes(strtoloer($filter))."%' OR lower(user_active) LIKE '%".addslashes(strtoloer($filter))."%' )";
			} else if($option=='SEARCH'){
				if($user_id!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(user_id) LIKE '%".addslashes(strtolower($user_id))."%'";
				};
				if($user_name!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(user_name) LIKE '%".addslashes(strtolower($user_name))."%'";
				};
				if($user_password!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(user_password) LIKE '%".addslashes(strtolower($user_password))."%'";
				};
				if($m_staff_id!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(m_staff_id) LIKE '%".addslashes(strtolower($m_staff_id))."%'";
				};
				if($m_usergroup_id!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(m_usergroup_id) LIKE '%".addslashes(strtolower($m_usergroup_id))."%'";
				};
				if($user_active!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(user_active) LIKE '%".addslashes(strtolower($user_active))."%'";
				};
			}
			$query = $this->db->query($sql);
			return $query->result();
		}

		//function  for export to excel
		function m_user_export_excel($user_id ,$user_name ,$user_password ,$m_staff_id ,$m_usergroup_id ,$user_active ,$option,$filter){
			//full query
			$sql = "SELECT user_id,
							 user_name,
							 user_password,
							 m_staff_id,
							 m_usergroup_id,
							 user_active					  FROM 	 m_user";
			if($option=='LIST'){
				$sql .=eregi("WHERE",$sql)? " AND ":" WHERE ";
				$sql .= "(lower(user_id) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(user_name) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(user_password) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(m_staff_id) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(m_usergroup_id) LIKE '%".addslashes(strtolower($filter))."%' OR
						 lower(user_active) LIKE '%".addslashes(strtolower($filter))."%' )";
			} else if($option=='SEARCH'){
				if($user_id!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(user_id) LIKE '%".addslashes(strtolower($user_id))."%'";
				};
				if($user_name!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(user_name) LIKE '%".addslashes(strtolower($user_name))."%'";
				};
				if($user_password!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(user_password) LIKE '%".addslashes(strtolower($user_password))."%'";
				};
				if($m_staff_id!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(m_staff_id) LIKE '%".addslashes(strtolower($m_staff_id))."%'";
				};
				if($m_usergroup_id!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(m_usergroup_id) LIKE '%".addslashes(strtolower($m_usergroup_id))."%'";
				};
				if($user_active!=''){
					$sql.=eregi("WHERE",$sql)?" AND ":" WHERE ";
					$sql.= " lower(user_active) LIKE '%".addslashes(strtolower($user_active))."%'";
				};
			}
			$query = $this->db->query($sql);
			return $sql;
		}

}
?>